package com.iot.core.data.mapper;


import com.iot.core.data.entity.HtTbid;

import com.iot.core.util.MyDateTime;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

import static com.iot.core.data.entity.table.HtTbidTableDef.HT_TBID;

@Mapper
public interface HtTbIdMapper extends MyBaseMapper<HtTbid> {
    default public    Long  nextDBID(String name) {
        var m= selectOneByCondition(HT_TBID.TNAME.eq(name));
        if (m.isIncludeDt())
        {
            if (m.isDayRestart())
            {
                if (!m.getCurrentDt().equals(MyDateTime.todayShort() ) )
                {
                    m.setCurrentDt(MyDateTime.todayShort());
                    m.setIdv(0L);
                }
            }
            else
            {

            }

            m.setIdv(m.getIdv()+1);
            var r =(long)  ( Long.parseLong(m.getCurrentDt() ) * Math.pow(10, m.getIdLength())+m.getIdv() );

            var um= UpdateEntity.of(HtTbid.class,m.getId() );
            um.setIdv(m.getIdv());
            update(um);

            return r;
        }
        else
        {
            m.setIdv(m.getIdv()+1);
            var r =m.getIdv();

            return r;
        }
    }
}

